//app.js
var util = require('./utils/util.js')
App({
  /**  
     * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）  
     */
  onLaunch: function () {


  },
  /**  
    * 当小程序启动，或从后台进入前台显示，会触发 onShow  
    */
  onShow: function (options) {
    // 获取用户信息  
    var user_info = wx.getStorageSync('user_info');
    var user_dtime = wx.getStorageSync('user_dtime');
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log(1111)
    console.log(user_info)
    console.log(user_dtime)
    console.log(1111)
    if (user_dtime && user_info) {
      var gtimestamp = timestamp - 60 * 60;
      if (gtimestamp > user_dtime) {
        this.getUserInfo()
      } else {
        this.globalData.userInfo = user_info
      }
    } else {
      this.getUserInfo()
    }
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      wx.clearStorage()
      // 调用登录接口  
      wx.login({
        success: function (loginRes) {
          if (loginRes.code) {
            wx.getUserInfo({
              success: res => {
                console.log(res);
                var data = {
                  code: loginRes.code,
                  encrypted_data: res.encryptedData,
                  iv: res.iv,
                  raw_data: res.rawData,
                  signature: res.signature
                }
                util.commonAjax('/wxapp/public/login', 2, data)
                  .then(function (data) {
                    var data = data.data
                    if (data.code == 1) {
                      wx.showToast({
                        title: '登录成功!',
                        icon: 'success',
                        duration: 1000
                      });
                      var user_info = {
                        token: data.data.token,
                        avatar: data.data.user.avatar,
                        user_nickname: data.data.user.user_nickname,
                        user_id: data.data.user.id
                      }
                      that.globalData.userInfo = user_info
                      //写入缓存                     
                                         
                      wx.setStorageSync('user_info', user_info)
                      var timestamp = Date.parse(new Date());
                      timestamp = timestamp / 1000;
                      wx.setStorageSync('user_dtime', timestamp)
                      console.log(222)
                      console.log(user_info)
                      console.log(timestamp)
                      console.log(2222)
                    } else {
                      wx.showToast({
                        title: data.msg,
                        icon: 'loading',
                        duration: 1000
                      });
                    }
                  })
              },
              fail: (res) => {
                console.log(res);
                if (res.errMsg == "getUserInfo:cancel" || res.errMsg == "getUserInfo:fail auth deny") {
                  wx.showModal({
                    title: '用户授权失败',
                    showCancel: false,
                    content: '请删除此小程序重新授权!',
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      }
                    }
                  });
                }
              }
            });


          } else {
            tryingLogin = false;
          }
        }
      })
    }
  },
  /**  
  * 全局变量配置（在这里放一些常量和配置文件，如果公共参数多的话大家也可以去重新布局一个文件，在里面进行设置）  
  */
  globalData: {
    userInfo: null
  }
})